#include "Server.h"

#include <utility>

Server::Server(ServerConfig config) : svrConfig(std::move(config)) {
    if (svrConfig._success.first) {
        bool bind_succ = this->bind_to_port(svrConfig.host, svrConfig.listen_port);
        if (!bind_succ) {
            Loge(to_string(svrConfig.listen_port) + "端口绑定失败");
            exit(EXIT_FAILURE);
        }

        for (auto &_: ServerHandle::handles) {
            switch (_.type) {
                case GET:this->Get(_.path, _.func);
                    break;

                case POST:this->Post(_.path, _.func);
                    break;

                default:break;
            }
        }

        this->Get(send_text_path, send_text_GET);
        Logi("服务器初始化完成");
        Logi("监听地址：" + svrConfig.host);
        Logi("监听端口：" + to_string(svrConfig.listen_port));
    }
    else {
        Loge(svrConfig._success.second);
        exit(EXIT_FAILURE);
    }
}

bool Server::listen() {
    return this->listen_after_bind();
}

Server::~Server() {
    this->stop();
}
